package dfs_bfs.test341;

import listbyorder.access301_400.test341.NestedInteger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 扁平化嵌套列表迭代器
 * @author code_yc
 * @version 1.0
 * @date 2020/7/8 15:43
 */
public class Solution1 {

    public class NestedIterator implements Iterator<Integer> {

        List<Integer> seq;
        int count;

        public NestedIterator(List<NestedInteger> nestedList) {
            seq = new ArrayList<>();
            count = 0;
            dfs(nestedList);
        }

        private void dfs(List<NestedInteger> nestedList) {
            for(NestedInteger n : nestedList){
                if(n.isInteger()){
                    seq.add(n.getInteger());
                }else{
                    dfs(n.getList());
                }
            }
        }

        @Override
        public Integer next() {
            return seq.get(count++);
        }

        @Override
        public boolean hasNext() {
            return count < seq.size();
        }
    }
}
